package org.kalai.unifypay.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.mins.MinsPay;
import org.kalai.unifypay.util.CommonUtil;
import org.kalai.unifypay.util.DataDao;
import org.kalai.unifypay.util.StringUtil;

import com.alibaba.fastjson.JSON;

/**
 * 后台真实商户注册(民生)
 * @author zsl-2016年9月26日(修改)
 *
 */
@WebServlet(urlPatterns="/AlipayRegister")
public class AlipayRegister extends HttpServlet {
	private static final Logger logger = Logger.getLogger(AlipayRegister.class);
	private static final long serialVersionUID = -5727519623795411961L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CommonUtil.responseObject(resp, Constant.ERROR_REQEST);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		Map<String, Object> result = CommonUtil.createRespMap("00", "商户注册成功");
		try {
			String reqMsgId = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
			String merchantId = request.getParameter("merchantId");//商户号
			String merchantName = request.getParameter("merchantName");//商户名称
			String shortName = request.getParameter("shortName");//商户简称
			String merchantAddress = request.getParameter("merchantAddress");//商户地址
			String servicePhone = request.getParameter("servicePhone");//客服电话
			String category = request.getParameter("category");//经营类目  详见 支付宝-全行业类目表.xls
			String type = request.getParameter("type");
			String isReal = request.getParameter("isReal");
			if (StringUtil.isEmpty(merchantId, merchantName, shortName, merchantAddress, servicePhone, category, type)) {
				logger.error(String.format("业务参数错误：merchantId=%s, merchantName=%s, shortName=%s, merchantAddress=%s, servicePhone=%s, category=%s", 
						merchantId, merchantName, shortName, merchantAddress, servicePhone, category));
				result = CommonUtil.createRespMap("200", "业务参数错误");
				return;
			}
			result = MinsPay.merchantApply(Integer.valueOf(type), reqMsgId, merchantId, merchantName, shortName, merchantAddress, servicePhone, category);
			if(result.get("respCode").equals("00")){
				//商户注册成功
				String merchantCode = result.get("merchantCode").toString();
				DataDao.getInstance().addAlipayMerchant(merchantId, merchantName, shortName, merchantAddress, servicePhone, category, type, merchantCode,isReal);
			}else{
				logger.error(String.format("商户注册失败:[%s][%s][%s]", merchantId, merchantName, JSON.toJSONString(result)));
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			result = CommonUtil.createRespMap("10", e.getMessage());
		} finally {
			CommonUtil.responseObject(response, JSON.toJSONString(result));
		}
	}

}
